package com.lyy.blog.dao;

import com.lyy.blog.po.blog;
import org.hibernate.query.NativeQuery;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface blogrepositroy extends JpaRepository<blog, Long>, JpaSpecificationExecutor<blog> {

    @Query("select b from blog b where b.recommend=true")
    List<blog> findTop(Pageable pageable);


    @Query("select b from blog b where b.title like ?1 or b.content like ?1")
    Page<blog> findByQuery(String query,Pageable pageable);

    @Transactional
    @Modifying
    @Query("update blog b set b.views = b.views+1 where b.id = ?1")
    int updateViews(Long id);


    @Query("select function('date_format',b.updateTime,'%Y') as year from blog b group by function('date_format',b.updateTime,'%Y') order by year desc ")
    List<String> findGroupYear();


    @Query("select b from blog b where function('date_format',b.updateTime,'%Y') = ?1")
    List<blog> findByYear(String year);





}
